<?php

namespace App\Presenters;

use App\Repositories\PermissionRepositoryEloquent;
use App\Transformers\PermissionTransformer;
use Prettus\Repository\Presenter\FractalPresenter;
use Route,Auth;
use App\Models\Permission;

/**
 * Class PermissionPresenter
 *
 * @package namespace App\Presenters;
 */
class PermissionPresenter extends FractalPresenter
{
    protected $permission;

    public function __construct(PermissionRepositoryEloquent $permission)
    {
        $this->permission = $permission;
    }


    /**
     * Transformer
     *
     * @return \League\Fractal\TransformerAbstract
     */
    public function getTransformer()
    {
        return new PermissionTransformer();
    }

    /**
     * top permission select
     * @param int $fid
     * @return string
     */
    public function topPermissionSelect($fid = 0)
    {
        $tops = $this->permission->topPermissions();
        //$tops = $this->permission->menuPermissions();
        $select = '<select class="form-control input-sm" name="fid">';
        $select .= '<option value="0">--顶级权限--</option>';
        if($tops->count()) {
            foreach ($tops as $top) {
                if($top->id == $fid) {
                    $select .= '<option  value="' . $top->id . '" selected >' . $top->name . '[' . $top->slug . ']</option>';
                } elseif($top->id != $fid) {
                    $select .= '<option  value="' . $top->id . '">' . $top->name . '[' . $top->slug . ']</option>';
                }
            }
        }
        $select .= '</select>';
        return $select;
    }

    /**
     * 用户根据权限可见的菜单
     * @return string
     */
    public function menus()
    {
        $menus = $this->permission->menus();
        $html = '';
        if($menus) {
            //dd($menus);
            foreach ($menus as $k=>$menu) {
                //dd($menu);
                // if(($menu['slug'] !== '#') && !Route::has($menu['slug'])) {
                //     continue;
                // }

                if((stripos($menu['slug'], '#') === false) && !Route::has($menu['slug'])) {
                    continue;
                }


                $class = '';
                if(isset($menu['sub'])) {
                    $class .= ' sub-menu nav-parent';
                }
                $style='';
                $active = '';
                //echo $menu['name'].'-'.Route::currentRouteName(),'<hr>';
                if($menu['slug'] == Route::currentRouteName()) {
                    $class .= ' active';
                    $active = ' active';
                    $style="style='display: block;'";
                }

                if(!Auth::user()->can($menu['slug'])){
                    $class .= ' hide';
                }
                if(isset($menu['sub'])) {
                    foreach ($menu['sub'] as $sub) {
                        //if($sub['slug'] == Route::currentRouteName()) {
                       //var_dump($sub['slug']);
                        //var_dump(Route::currentRouteName());
                        if((stripos(Route::currentRouteName(),$sub['slug']) !== false) ) {

                            $class .= ' active';
                            $active = ' active';
                            //$style="style='display: block;'";
                        }
                    }
                }
                $p = Permission::where('slug','=',Route::currentRouteName())->first();
               //var_dump($menu);
                //dd($p->fid);
                
                if($p && $p->fid==$menu['id']){
                    $class .= ' active';
                    $active = ' active';
                }
                
                //获取当前路径的父级 如果名称相同加上参数

                //echo $k.'-'.$class.'<hr>';
                $html .= '<li class="' . $class . '">';
                //$href = ($menu['slug'] == '#') ? '' : route($menu['slug']);
                $href = (stripos($menu['slug'], '#') !== false) ? '' : route($menu['slug']);
                //$html .= sprintf('<a class="menu-a'.$active.'" href="%s">%s <spa
                $html .= sprintf('<a class="menu-a'.$active.'" href="%s">%s <span>%s</span></a>', $href, $menu['icon_html'], $menu['name']);

                if(!isset($menu['sub'])) {
                    $html .= '</li>';
                    continue;
                }
                //dd($html);
                $html .= "<ul class='sub' >";
                foreach ($menu['sub'] as $sub) {
                    //echo 11;
                    //if(($sub['slug'] !== '#') && !Route::has($sub['slug'])) {
                    if((stripos($sub['slug'], '#') === false) && !Route::has($sub['slug'])) {
                        continue;
                    }
                    //$href = ($sub['slug'] == '#') ? '#' : route($sub['slug']);
                    //echo 1;
                    $href = (stripos($sub['slug'], '#') !== false) ? '#' : route($sub['slug']);
                    $icon = $sub['icon_html'] ? $sub['icon_html'] : '<i class="fa fa-caret-right"></i>';

                    if(!Auth::user()->can($sub['slug'])){
                        $html .= sprintf('<li class="sub-menus hide"><a class="sub-menu-a" href="%s">%s %s</a></li>', $href, $icon, $sub['name']);
                    }else{
                        //var_dump($sub['slug']);
                        if((stripos(Route::currentRouteName(),$sub['slug']) !== false) ) {
                            //dd(Route::currentRouteName());
                            $html .= sprintf('<li class="sub-menus active"><a class="sub-menu-a" href="%s">%s %s</a></li>', $href, $icon, $sub['name']);
                        }else{
                            $html .= sprintf('<li class="sub-menus"><a class="sub-menu-a" href="%s">%s %s</a></li>', $href, $icon, $sub['name']);
                        }
                    }
                }
                $html .= '</ul>';
                $html .= '</li>';

            }
        }
        //dd($html);
        return $html;
    }
}
